﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HealthHelper
{
    public partial class FrmRest : Form
    {
        public FrmRest()
        {
            InitializeComponent();
            p_top.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance |
                      System.Reflection.BindingFlags.NonPublic).SetValue(p_top, true, null);
        }

        List<Form> otherScreenFrmList = new List<Form>();

        private void FrmRest_Load(object sender, EventArgs e)
        {
            Screen[] screenList = Screen.AllScreens;

            foreach (Screen screen in screenList)
            {

                if (screen.Primary)
                {
                    FullScreen(this, screen);
                }
                else
                {
                    // 副屏幕
                    Form frm = new FrmScreen();
                    frm.Show();
                    otherScreenFrmList.Add(frm);
                    frm.FormClosing += Frm_FormClosing;
                    FullScreen(frm, screen);

                }
                p_CenterMsg.Location = new Point((this.Width - p_CenterMsg.Width) / 2, (this.Height - p_CenterMsg.Height) / 2);
            }

        }

        bool closeAll = false;

        private void Frm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Console.WriteLine(sender);
            // 主屏关闭事件 
            if (sender == this)
            {
                if (!closeAll)
                {
                    closeAll = true;
                    otherScreenFrmList.ForEach(p =>
                    {
                        this.Invoke(new Action(() =>
                        {
                            p.Close();
                        }));
                    });
                }
            }
            else
            {
                // 当不是全部关闭的时候，说明是当前副屏触发关闭事件，移出当前副屏，关闭主屏
                if (!closeAll)
                {
                    otherScreenFrmList.Remove(sender as Form);
                    this.Close();
                }
            }

        }

        private void FullScreen(Form frm, Screen screen)
        {
            frm.DesktopBounds = screen.Bounds;
            frm.TopMost = true;
            frm.FormBorderStyle = FormBorderStyle.None;
            frm.WindowState = FormWindowState.Maximized;
        }

        private void btn_NowWork_BtnClick(object sender, EventArgs e)
        {
            this.Close();
        }
        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            int WM_KEYDOWN = 256;
            int WM_SYSKEYDOWN = 260;
            if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
            {
                switch (keyData)
                {
                    case Keys.Escape:
                        this.Close();//esc关闭窗体
                        break;
                }
            }
            return false;
        }


        public void RefreshRestTime(string countDown, int restProgress)
        {
            this.Invoke(new Action(() =>
            {
                lb_CountDownSecond.Text = countDown;
                pl_Rest.Value = restProgress;
            }));
        }

        private void FrmRest_Shown(object sender, EventArgs e)
        {
            p_CenterMsg.Visible = true;
        }
    }
}
